import time


def get_time_text(time_ticks):
    """将时间戳装换成当前相对时间"""
    today = time.localtime(time.time())
    thatday = time.localtime(int(time_ticks))
    if not thatday.tm_year == today.tm_year:
        return "{:04d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}".format(
            thatday.tm_year, thatday.tm_mon, thatday.tm_mday,
            thatday.tm_hour, thatday.tm_min, thatday.tm_sec
        )
    elif not (today.tm_mday == thatday.tm_mday and today.tm_mon == thatday.tm_mon):
        err = int(time.time()) - int(time_ticks)
        if 172800 <= err <= 259200:  # 相差两天
            return u"前天 {:02d}:{:02d}:{:02d}".format(
                thatday.tm_hour, thatday.tm_min, thatday.tm_sec
            )
        elif 86400 <= err <= 172800:  # 相差一天
            return u"昨天 {:02d}:{:02d}:{:02d}".format(
                thatday.tm_hour, thatday.tm_min, thatday.tm_sec
            )
        else:
            return "{:02d}-{:02d} {:02d}:{:02d}:{:02d}".format(
                thatday.tm_mon, thatday.tm_mday,
                thatday.tm_hour, thatday.tm_min, thatday.tm_sec
            )
    else:
        if thatday.tm_hour < 12:
            return u"上午 {:02d}:{:02d}:{:02d}".format(
                thatday.tm_hour, thatday.tm_min, thatday.tm_sec
            )
        else:
            return u"下午 {:02d}:{:02d}:{:02d}".format(
                thatday.tm_hour, thatday.tm_min, thatday.tm_sec
            )


def is_need_show_time(old, new, t=60):
    if int(new) - int(old) > t:
        return True
    else:
        return False


if __name__ == "__main__":
    #  测试时间 2023-01-06
    time1 = "1672502400"  # "2023-01-01 00:00:00"  # 同年不同日期
    time2 = "1640966400"  # "2022-01-01 00:00:00"  # 不同年
    time3 = "1672989034"  # "2023-01-06 15:10:34"  # 同天下午
    time4 = "1672953034"  # "2023-01-06 05:10:34"  # 同天上午
    time5 = "1672848000"  # "2023-01-05 00:00:00"  # 昨天
    time6 = "1672761600"  # "2023-01-04 00:00:00"  # 前天
    print(get_time_text(time1))
    print(get_time_text(time2))
    print(get_time_text(time3))
    print(get_time_text(time4))
    print(get_time_text(time5))
    print(get_time_text(time6))
